package com.cdream.basic.algorithm.leetcode.map;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author cdream
 * @date 2021/9/17
 */
public class Question36 {
    public boolean isValidSudoku(char[][] board) {
        int[][] row = new int[9][9];
        int[][] column = new int[9][9];
        int[][][] small = new int[3][3][9];

        for (int i = 0; i < 9; i++) {
            for (int j = 0; j < 9; j++) {
                int ele = board[i][j];
                if (ele == '.') {
                    continue;
                }
                int index = ele - '1';
                row[i][index]++;
                column[j][index]++;
                small[i / 3][j / 3][index]++;
                if (row[i][index] > 1 || column[j][index] > 1 || small[i / 3][j / 3][index] > 1) {
                    return false;
                }
            }
        }
        return true;
    }
    public String destCity(List<List<String>> paths) {
        Map<String, String> map = paths.stream().collect(Collectors.toMap(a->a.get(0), a->a.get(1)));
        for(String key : map.keySet()){
            if(!map.containsKey(map.get(key))) {
                return map.get(key);
            }
        }
        return "";
    }
    public int thirdMax(int[] nums) {
        List<Long> l = Arrays.stream(nums).mapToLong(a-> (long) a).boxed().distinct().sorted((a,b)->Long.compare(b,a)).collect(Collectors.toList());
        return l.size()>=3? l.get(2).intValue() : l.get(l.size()-1).intValue();
    }
    public int countSegments(String s) {
        s = s.trim();
        if(s.length()==0){
            return 0;
        }
        return (int)Arrays.stream(s.split(" ")).filter(a-> !a.equals("")).count();
    }
}
